package com.bwda.dsrs.filecenter.util;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;

import java.util.Map;

/**
 * simple introduction
 *
 * @author yanyuemei
 * @date
 */
public class JwtTokenUtil {
    private static final Logger logger = LoggerFactory.getLogger(JwtTokenUtil.class);
    private static final int BEARER_BEGIN_INDEX = 7;

    /**
     * jwt token 密钥，主要用于token解析，签名验证
     */
    @Value("${spring.security.oauth2.jwt.signingKey}")
    private String signingKey;

    /**
     * Authorization认证开头是"bearer "
     */
    public static Jwt getJwt(String authentication) {
        return JwtHelper.decode(StringUtils.substring(authentication, BEARER_BEGIN_INDEX));
    }

    /**
     * 过期时间5分钟
     */
    private static final long EXPIRE_TIME = 5 * 60 * 1000;

    /**
     * 校验token是否正确
     *
     * @param token  密钥
     * @param secret 用户的密码
     * @return 是否正确
     */
//    public static boolean verify(String token, String username, String secret) {
//        try {
//            Algorithm algorithm = Algorithm.HMAC256(secret);
//            JWTVerifier verifier = JWT.require(algorithm)
//                    .withClaim("username", username)
//                    .build();
//            DecodedJWT jwt = verifier.verify(token);
//            return true;
//        } catch (Exception exception) {
//            return false;
//        }
//    }

    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */

    public static String getUserCode(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            Map userDetail = (Map) JSON.parseObject(jwt.getClaim("userDetail").asString());
            return (String) userDetail.get("userCode");
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static String getUserName(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            Map userDetail = (Map) JSON.parseObject(jwt.getClaim("userDetail").asString());
            return (String) userDetail.get("userName");
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static String getOrgId(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            Map userDetail = (Map) JSON.parseObject(jwt.getClaim("userDetail").asString());
            return String.valueOf(userDetail.get("orgId"));
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static String getOrgName(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            Map userDetail = (Map) JSON.parseObject(jwt.getClaim("userDetail").asString());
            return (String) userDetail.get("orgName");
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static String getOrgAdminCode(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            Map userDetail = (Map) JSON.parseObject(jwt.getClaim("userDetail").asString());
            return (String) userDetail.get("orgAdminCode");
        } catch (JWTDecodeException e) {
            return null;
        }
    }

}
